<?php
/**
 * This file belongs to huakaili project
 *
 * @author jszhang
 */

global $logid;

$pageVars = array();
$errmsg = GetMessageStack();
$userDao = GetUserDAO();

//write to database
if(isset($_POST['action'])) {
	$action=trim($_POST['action']);
	if($action=='msg_setting') {
		//use loop to ensure single entrance and single exit
		do{
			$params = array();
			//$params['id'] = intval($_POST['id']);
			$params['id'] = $logid;
			$params['add_shop'] = intval($_POST['add_shop']);
			$params['correct_shop'] = intval($_POST['correct_shop']);
			$params['correct_product'] = intval($_POST['correct_product']);
			$params['shop_corrected'] = intval($_POST['shop_corrected']);
			$params['product_corrected'] = intval($_POST['product_corrected']);
			$params['friend_added'] = intval($_POST['friend_added']);
			$params['contribution_change'] = intval($_POST['contribution_change']);

			//first check params
			if( edp_is_empty_string($params['id'] ) )
				$errmsg->add( 'msg_setting', '用户不合法！', 'error' );

			$pageVars = array_merge( $pageVars, $params );
			// when there is error, break the loop
			if( $errmsg->size('msg_setting') > 0 )
			{
				break;
			}

			if( !$userDao->UpdateUserMsgSetting( $params ) ) {
				$errmsg->add( 'msg_setting', '写入数据库失败', 'error' );
			}
			else{
				$errmsg->add( 'msg_setting', '更新成功', 'success' );
			}

			break;
		}while(1);
	}
	else{
		$errmsg->add( 'msg_setting', '未知操作', 'error' );
	}
}
else{
	/*
	$userInfo = getUserFromCookie();
	if( $userInfo ){
		//get user basic_info from database
		 */
		$userMsgSetting = $userDao->GetUserMsgSetting( $logid);
		if( $userMsgSetting ){
			$pageVars['id'] = $userMsgSetting['user_id'];
			$pageVars['add_shop'] = intval($userMsgSetting['add_shop']);
			$pageVars['correct_shop	'] = intval($userMsgSetting['correct_shop']);
			$pageVars['correct_product'] = intval($userMsgSetting['correct_product']);
			$pageVars['shop_corrected'] = intval($userMsgSetting['shop_corrected']);
			$pageVars['product_corrected'] = intval($userMsgSetting['product_corrected']);
			$pageVars['friend_added'] = intval($userMsgSetting['friend_added']);
			$pageVars['contribution_change'] = intval($userMsgSetting['contribution_change']);
		}
		else{
			$errmsg->add( 'msg_setting', '读取用户信息失败', 'error' );
		}
		/*
	}
	else{
		$errmsg->add( 'msg_setting', '登录用户信息不合法', 'error' );
	}
	*/
}
?>